package Groep1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;


public class SmtpTalk {
  BufferedReader is;
  PrintStream os;
  private boolean debug = true;
  private String host;


  /**
   * Constructor taking a server hostname as argument.
   */
  public SmtpTalk(String server) throws Exception {
    host = server;
    try {
      Socket s = new Socket(host, 25);
      is = new BufferedReader(new InputStreamReader(s.getInputStream()));
      os = new PrintStream(s.getOutputStream());
    } catch (NoRouteToHostException e) {
      die( "No route to host " + host);
    } catch (ConnectException e) {
      die("Connection Refused by " + host);
    } catch (UnknownHostException e) {
      die("Unknown host " + host);
    } catch (IOException e) {
      die( "I/O error setting up socket streams\n" + e);
    }
  }

  /** Send a command with an operand */
  protected void send_cmd(String cmd, String oprnd) {
    send_cmd(cmd + " " + oprnd);
  }

  /* Send a command with no operand */
  protected void send_cmd(String cmd) {
    if (debug)
      System.out.println(">>> " + cmd);
    os.print(cmd + "\r\n");
  }

  /** Send_text sends the body of the message. */
  public void send_text(String text) {
    os.print(text + "\r\n");
  }

  /** Expect (read and check for) a given reply */
  protected boolean expect_reply(String rspNum) throws Exception {
    String s = null;
    try {
      s = is.readLine();
      while( s.startsWith(rspNum + "-" ) ){
    	  s = is.readLine();
      }
    } catch (IOException e) {
      die( "I/O error reading from host " + host + " " + e);
    }
    if (debug)
      System.out.println("<<< " + s);
    return s.startsWith(rspNum + " ");
  }

  protected void die(String msg) throws Exception {
    throw new Exception( msg);
  }

  /**
   * send Mail message to recipients via smtp to server
   * "host".
   */
  public void converse(String sender, String recipients, String subject,
      String body) throws Exception {

    if (!expect_reply("220"))
      die("did not get SMTP greeting");

    send_cmd("HELO", "smtp.euronet.nl");
    if (!expect_reply("250"))
      die("did not ack our HELO");

    send_cmd("MAIL", "From:<" + sender + ">"); // no spaces!
    if (!expect_reply("250"))
      die("did not ack our MAIL command");

    StringTokenizer st = new StringTokenizer(recipients);
    while (st.hasMoreTokens()) {
      String r = st.nextToken();
      send_cmd("RCPT", "To:<" + r + ">");
      if (!expect_reply("250"))
        die("didn't ack RCPT " + r);
    }
    send_cmd("DATA");
    if (!expect_reply("354"))
      die("did not want our DATA!");

    send_text("From: " + sender);
    send_text("To: " + recipients);
    send_text("Subject: " + subject);
    send_text("");
    send_text(body + "\r");

    send_cmd(".");
    if (!expect_reply("250"))
      die("Mail not accepted");

    send_cmd("QUIT");
    if (!expect_reply("221"))
      die("Other end not closing down");
  }
}